<template>
	<view>
		<uni-section title="选择时间" type="line" titleFontSize="20px">
			<uni-card>
				<view class="uni-list">
					<view class="uni-list-cell">
						<view class="uni-list-cell-db">
							<picker mode="time" :value="time" start="00:00" end="23:59" @change="bindTimeChange">
								<view class="uni-input" style="font-size: 50rpx;height: 50px;">{{ time }}</view>
							</picker>
						</view>
					</view>
				</view>
			</uni-card>

			<!-- <uni-card>
				<view @click="inputDialogToggle" style="font-size: 50rpx;height: 50px;" class="card-content">重复</view>

			</uni-card> -->

			<button style="margin: 0 auto;" class="button popup-success" @click="messageToggle('success')"><text
					class="button-text success-text">保存</text></button>
		</uni-section>
		<uni-popup ref="inputDialog" type="dialog">
			<view class="uni-list" style="width: 130px;border-radius: 10px;">
				<checkbox-group v-model="selectedDays">
					<label class="uni-list-cell uni-list-cell-pd" v-for="day in dayList" :key="day">
						<view>
							<checkbox :value="day" :checked="selectedDays.includes(day)"
								@change="onCheckboxChange(day)" />
						</view>
						<view>{{ day }}</view>
					</label>
				</checkbox-group>
			</view>
		</uni-popup>
		<view>
			<!-- 提示信息弹窗 -->
			<uni-popup ref="message">
				<uni-popup-message :type="msgType" message="保存成功" :duration="2000"></uni-popup-message>
			</uni-popup>
		</view>
	</view>

	</view>
</template>

<script>
	import {
		setPillBox,
	} from "../api/pillbox.js";
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			});
			return {
				type: 'center',
				date: currentDate,
				time: '00:00',
				seconds: 0,
				timeInit: 0,
				msgType: 'success',
				messageText: '这是一条成功提示',
				dayList: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
				selectedDays: ['周一', '周二'],

			};
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		created() {

			const currentDate = new Date();
			const hours = currentDate.getHours();
			const minutes = currentDate.getMinutes();
			const formattedTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
			this.date = currentDate;
			this.time = formattedTime;

		},
		methods: {
			bindTimeChange(e) {
				this.time = e.detail.value;
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			inputDialogToggle() {
				this.$refs.inputDialog.open()
			},

			async messageToggle(type) {
		
				this.addClock()
				uni.showLoading({
					title:'加载中...'
				})
				const res = await setPillBox(this.addClock())
				uni.hideLoading();
				if (res.code !== 200) {
					uni.showToast({
						duration:1500,
						icon:'none',
						title:res.msg
					})
				}
				// 返回上个页面
				setTimeout(() => {
					this.$tab.navigateTo(`/pages/homepage/PillBox/index?uuid=${this.$mp.query.uuid}`);
				}, 400)
			},
			onCheckboxChange(day) {
				if (this.selectedDays.includes(day)) {
					const index = this.selectedDays.indexOf(day);
					if (index !== -1) {
						this.selectedDays.splice(index, 1);
					}
				} else {

					this.selectedDays.push(day);
				}
			},
			// 添加闹钟 最多九个闹钟
			addClock() {
				var date = new Date();
				for (let i = 1; i <= 9; i++) {
					if (JSON.parse(this.$mp.query.header).indexOf("alarm" + i) == -1) {
						const alarm = {
							tp: "alarm" + i,
							op: "on",
							da: `${date.getFullYear()}-${(date.getMonth()+1)}-${date.getDate()}`,
							tm: this.time,
							sta: "set",
							oldManId: this.$mp.query.uuid,
							topic: "P38:862170067052843",
						}
						return alarm;
					}
				}
			}
		}
	};
</script>

<style scoped>
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #fff;
		min-height: 100%;
		height: auto;
	}

	.uni-common-pl {
		padding-left: 30rpx;
	}

	.uni-title text {
		font-size: 24rpx;
		color: #888;
	}

	.uni-list-cell-left {
		white-space: nowrap;
		font-size: 40rpx;
		padding: 0 30rpx;
	}

	.uni-list {
		background-color: #FFFFFF;
		position: relative;
		width: 100%;
		display: flex;
		flex-direction: column;
	}

	.uni-list-cell {
		position: relative;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.uni-list:after {
		position: absolute;
		z-index: 10;
		right: 0;
		bottom: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		/* background-color: #c8c7cc; */
	}

	.uni-list-cell-pd {
		padding: 22rpx 30rpx;
	}

	.uni-list-cell-db,
	.uni-list-cell-right {
		flex: 1;
	}

	.uni-card {
		cursor: pointer;

		padding: 10px;

	}

	.selected-days {
		font-weight: bold;
		margin-left: 10px;
	}

	.uni-input {
		font-size: 40rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10px;
	}

	.repeat-label {
		font-weight: bold;
		margin-right: 10px;
		cursor: pointer;
	}

	.button {

		align-items: center;
		justify-content: center;
		flex: 1;
		height: 50px;
		margin: 0 5px;
		width: 100px;
		border-radius: 5px;
		bottom: 20px;
		position: fixed;
		left: 40%;
		/* 水平居中 */
	}

	.popup-success {
		color: #fff;
		background-color: #007BFF;
	}

	.button-text {
		color: #fff;
		font-size: 12px;
	}

	.card-content {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10px;
		/* border-bottom: 1px solid #e0e0e0; */
	}

	.success-text {
		color: #ffffff;
		font-size: 20px;

	}
</style>
